import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.net.ssl.*;

//import javax.annotation.processing.Processor;
import javax.imageio.ImageIO;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class main implements ActionListener ,MouseListener {

   //Socket 	   				socket 			= null;
   PrintWriter 				out				= null;
   ObjectOutputStream 		out1			=null ;
   BufferedReader 			in 				= null;
   private static 	String 	Role			=null;
   static 			main 	frame;
   					JFrame	mainframe		=new JFrame("Grade System");
   private static 	JPanel 	west 			= new JPanel();
   private static 	JPanel  center 			= new JPanel();
   private static	JPanel  cont		    =new JPanel();
   private static	JPanel  northjpanel		    =new JPanel();
   private static 	JButton	login			=new JButton();
   private static 	JLabel	UserName		=new JLabel("User Name");
   private static 	JLabel	Pass			=new JLabel("Password");

   private static	JTextField TextUserName	=new JTextField(15);
 
   private static	JPasswordField TextPass		=new JPasswordField(20);
   private static 	JPanel	   mainpanel 		= new JPanel();
   private static 	String		Accountid;
   private static 	String		userPass;
   private Student 			S				=new Student();
   private Professor		P			=new Professor();
   private TA				TA			=new TA();
   private static 	Object  stu;
   private static 	int		numberofroles;
   private static 	String[] Roles;
   public static    JComboBox RoleList	= new JComboBox();
   public static 	String[][] Courses;
   public static	JTextField Username 			= new JTextField(20);
   public static	JPasswordField UserPass 		= new JPasswordField(20);
   public static	JTextField Useremail			= new JTextField(20);
   public static	JTextField Userfullname 		= new JTextField(20);
   public static	JTextField Useraddress 			= new JTextField(20);
   public static	JTextField Userphone 			= new JTextField(20);
   public static	JButton    Rigester 			= new JButton("Rigester"); 
   public static 	JLabel 		StatusReport 				= new JLabel();
   public static 	JTextField txt 					= new  JTextField(25);
   public static 	JButton btn 					= new  JButton("Connect");
   public static JLabel lbl = new JLabel("Transcript");
   public static JLabel Statuslbl = new JLabel("Status:");
   boolean firsttime=true;
   JPanel pan = new JPanel();
   String s;
  
   
   main(){ //Begin Constructor
//	   firstpanel();
	   System.setProperty("javax.net.ssl.trustStore", "clientkey.jks");  
//	   System.out.println("fahad");
   }
   
	   public JPanel firstpanel()
	   {
		   pan.setVisible(false);
	   	Dimension FrameSize=new Dimension( 1100 , 600 );
  		mainframe.setSize( FrameSize );
  		login = new JButton( "Log in" );
  		mainframe.getContentPane().setPreferredSize( FrameSize );
  		mainframe.setLayout(new BorderLayout( ) );
  		//center.setLayout(new GridLayout( 8 , 4 ));
  		center.setBorder(BorderFactory.createTitledBorder("Login Form"));
  		center.add( UserName );
  		center.add( TextUserName);
  		center.add( Pass );
  		center.add( TextPass );
  		
  		center.add( login );
  		center.add(Statuslbl);
  		
  		
//  		 //add image
//		try {
//			BufferedImage myPicture = ImageIO.read(new File("/Users/hbargi/Documents/newGMS/C/src/bigrotation2.gif"));
//			JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
//	  		center.add( picLabel );
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//  	
  		
  		//center.add(RigesterAccountinterface());
  		login.addActionListener( this );
 		Rigester.addActionListener(this);
 		//btn.addActionListener(this);
  		mainpanel.setLayout(new BorderLayout());
  		mainpanel.add( center,BorderLayout.NORTH );
  		
  		mainpanel.add( RigesterAccountinterface(),BorderLayout.SOUTH );
  		mainpanel.add(pan, BorderLayout.CENTER);
  		//mainpanel.add( Rigester ,BorderLayout.SOUTH );
  		mainframe.getContentPane( ).add( mainpanel);
  		//mainframe.getContentPane( ).add( RigesterAccountinterface());
  		mainframe.setVisible( true );
  		
  		
  		return mainpanel;
	   }
	   public JPanel server()
	   {
		   //JPanel pan = new JPanel();
		   	Dimension FrameSize=new Dimension( 1100 , 600 );
	  		mainframe.setSize( FrameSize );
	  		mainpanel = new JPanel();
	  		mainframe.getContentPane().setPreferredSize( FrameSize );
	  		mainframe.setLayout(new BorderLayout( ) );
	  		//center.setLayout(new GridLayout( 8 , 4 ));
	  		pan.setBorder(BorderFactory.createTitledBorder("Login to Server"));
	  		//fahad
		pan.add(txt);
	    pan.add(btn);
	    pan.add(Statuslbl);
	    btn.addActionListener(this);
	    mainpanel.add(pan, BorderLayout.CENTER);
	    mainframe.getContentPane( ).add( mainpanel);
	    mainframe.setVisible( true );
	    return mainpanel;
	   }
  	
 public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if(source == btn )
    {
    	String serverip=txt.getText();
    	//if (serverip)
    	if(listenSocket(serverip))
	    	{
	    		pan.removeAll();
	    		firstpanel();
	    	}
    	else {
    		System.out.println("Unknown host UP...");
    		}
    }
    if(source == login){	
   	    	Accountid=TextUserName.getText();
   	    	userPass = TextPass.getText();
   	 		mainpanel.removeAll();
   	 		
   	 		try {
   	   	 		out.println("checkUser;" + Accountid + ";" + userPass);
				String lines = in.readLine();
				if (lines.equals("Success"))
				{
					System.out.println("Authenticated");
					
   	 				mainpanel.add( S.SMenueInterfacee( ) , BorderLayout.WEST );
   	 				out.println("login;"+Accountid+";get_courses");   	 				
   	 					String line = in.readLine();
   	 					numberofroles=Countrole(line);
			   	        mainpanel.removeAll();
			   	        SetRoles();
			   	        if (numberofroles!=0)
			   	        {
			   	        	
			   	        	northjpanel=Northmenue( );
			   	         Role=Roles[0];
			   	         RoleList.setSelectedIndex(0);
			   	         RoleList.addActionListener(this);
				   	     source=RoleList;
				   	     P.Courses=Courses;
			   	         TA.Courses=Courses;
			   	      if (Role.equals("Professor"))
			   	    	source= P.ShowCorses;
					   	  for(int i=0;i<Roles.length;i++)
			   				{
					   		  
			   	        	if (Roles[i].equals("Professor"))
			   	        		P.addcourses();
			   	        	else if (Roles[i].equals("Ta"))
			   	        		TA.addcourses();
			   	      
			   				}
			   	        }
			   	        else
			   	        	{
			   	        	//mainpanel.removeAll();
			   	        	//center.removeAll();
			   	        	S.SMenueInterface().removeAll();
			   	        	mainpanel.add( S.SMenueInterfacee( ) , BorderLayout.WEST );
			   	        	//S.SMenueInterfacee( );
			   	        	northjpanel=Northmenuee( );
			  
			   	        	//Statuslbl.setText("Error: No roles assigned.");
			   	        	}
			   	       
			   	       
			   	   
				   	    mainpanel.add(northjpanel , BorderLayout.NORTH );
			   	        System.out.println(line);
			   	      	//mainpanel.add( S.SMenueInterface( ) , BorderLayout.WEST );
			   	     
			   	      	//mainpanel.add(S.g)
				}
				else if(lines.equals("The Username and Password don't Match")) { 
					System.out.println("Not Authenticated");
					center.removeAll();
					firstpanel().setVisible(true);
					Statuslbl.setText("Error: The Username and Password don't Match.");
				
				}
				else if(lines.equals("Your Account is Locked out")) { 
					System.out.println("Not Authenticated: Your Account is Locked out");
					center.removeAll();
					firstpanel().setVisible(true);
					Statuslbl.setText("Error: Your Account is Locked Out. Please Contact The Admin.");
				}
				else if(lines.equals("You're Not a Registered User")) { 
					System.out.println("Not Authenticated");
					center.removeAll();
					firstpanel().setVisible(true);
					Statuslbl.setText("Error: You're Not a Registered User.");
				}
				
   	 	} catch (IOException e1) {
   	 	//System.exit(1);
			firstpanel();
			Statuslbl.setText("No roles assigned");
			e1.printStackTrace();
		}
   	 		mainpanel.revalidate();
        	mainpanel.repaint();
        	//action listener 
        	S.ViewTran.addActionListener( this );
	 		S.PII.addActionListener( this );
	 		S.PIII.addActionListener( this );
	 		S.Logout.addActionListener( this );    
	 		P.ShowCorses.addActionListener(this);		
	 		P.SubmitGrade.addActionListener(this);
	 		P.PII.addActionListener(this);			
	 		P.Logout.addActionListener(this);		
	 		P.EditbPII.addActionListener(this); 
	 		TA.ShowCorses.addActionListener(this);		
	 		TA.SubmitGrade.addActionListener(this);
	 		TA.PII.addActionListener(this);			
	 		TA.Logout.addActionListener(this);		
	 		TA.EditbPII.addActionListener(this); 
	 		P.DataBaseTable.addMouseListener(this);
			P.ProfCoursList.addActionListener(this);
			P.ProfCoursList2.addActionListener(this);
			TA.DataBaseTable.addMouseListener(this);
	 		//TA.submitbtn.addActionListener(this);
	 		TA.ProfCoursList.addActionListener(this);
	 		P.CourseLog.addActionListener(this);
	 		P.proflogtable.addMouseListener(this);

	 		}
    else if (source == Rigester)
    {
    	server().setVisible(false);
      try{
    	  if((Username.getText().isEmpty()) || (UserPass.getText().isEmpty()) || (Useremail.getText().isEmpty()) || (Userfullname.getText().isEmpty()) || (Useraddress.getText().isEmpty()) || (Userphone.getText().isEmpty()))
  		{
      		StatusReport.setText("Please fill out all fields.");
  		}
  		else {
        out.println("Rigester;" + Username.getText().toString() + ";" + UserPass.getText().toString() + ";" + Useremail.getText().toString() + ";" + Userfullname.getText().toString() + ";" + Useraddress.getText().toString() + ";" + Userphone.getText().toString() );
        StatusReport.setText("Your request submitted successfully.");
        String line = in.readLine();
  	   	StatusReport.setText(line);
  		}
     	   
//          mainpanel.removeAll();
//	   	   mainpanel.add( Northmenue( ) , BorderLayout.NORTH );
//     	   mainpanel.add( S.getaction( line ), BorderLayout.CENTER );
//     	   mainpanel.add( S.SMenueInterface( ) , BorderLayout.WEST );
 		 } catch (Exception e){
 			 System.out.println("Read failed");
        	 System.exit(1);
      }
   	finally{
   		//StatusReport.setText("Your request sent to the system administrator for verifcation purposes.");
   		mainpanel.revalidate();
       	mainpanel.repaint();
   	}
   }
    
    else if(source == S.ViewTran)
    {
    	
    	out.println("S;"+Accountid+";get_transcript");
    	try{
      	   String line = in.readLine();
      	   System.out.println(line);
//      	 System.out.println(line);
//      	 JPanel panel = new JPanel();
//      	 lbl.setText(line);
//      	 	panel.add(lbl);
           mainpanel.removeAll();
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
      	   mainpanel.add( S.getaction( line ), BorderLayout.CENTER );
      	   mainpanel.add( S.SMenueInterface( ) , BorderLayout.WEST );
      	   //mainpanel.add(panel,BorderLayout.CENTER );
  		 } catch (IOException e){
  			 System.out.println("Read failed");
         	 System.exit(1);
         }
    	finally{
    		mainpanel.revalidate();
        	mainpanel.repaint();
    	}
    }
    
    if(source == S.PII)
    {
    	
    	out.println("S;" + Accountid + ";GET_PII");
    	try{
      	   String line = in.readLine();
           mainpanel.removeAll();
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
      	   mainpanel.add( S.getaction( line ), BorderLayout.CENTER );
      	   mainpanel.add( S.SMenueInterface( ) , BorderLayout.WEST );
  		 } catch (IOException e){
  			 System.out.println("Read failed");
         	 System.exit(1);
         }
    	finally{
    		mainpanel.revalidate();
        	mainpanel.repaint();
    	}
    }
    if(source == S.PIII)
    {
    	
    	out.println("S;" + Accountid + ";GET_PII");
    	try{
      	   String line = in.readLine();
           mainpanel.removeAll();
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
      	   mainpanel.add( S.getaction( line ), BorderLayout.CENTER );
      	   mainpanel.add( S.SMenueInterfacee( ) , BorderLayout.WEST );
  		 } catch (IOException e){
  			 System.out.println("Read failed");
         	 System.exit(1);
         }
    	finally{
    		mainpanel.revalidate();
        	mainpanel.repaint();
    	}
    }
    if( source==P.PII )
    {
    	out.println("S;" + Accountid + ";GET_PII");
    	try{
      	   String line = in.readLine();
           mainpanel.removeAll();
	   	   mainpanel.add( northjpanel , BorderLayout.NORTH );
      	   mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
      	   mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
  		 } catch (IOException e){
  			 System.out.println("Read failed");
         	 System.exit( 1 );
         }
    	finally{
    		mainpanel.revalidate();
        	mainpanel.repaint();
    	}
    }
    if( source == P.ShowCorses )
    {
    	P.check=false;
    	String courseid = null;
    	for (int i=0;i<Courses.length;i++)
    	{	
       		if (Courses[i][1].equals("Professor")){
    				courseid=Courses[i][0];
    					break;
    			}
    	    	
    	}
    	out.println("P;"+Accountid+";Get_Students;"+courseid);
		try{
			String line = in.readLine();
			System.out.println(line);
			mainpanel.removeAll();
			
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
	   	   
	   	    mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
	     	mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
		} catch (IOException e){
			 System.out.println("Read failed");
     	 System.exit(1);
		}
//		if (firsttime){
		P.submitbtn.addActionListener(this);

		//P.DataBaseTable.addMouseListener(this);
//		}
		
		firsttime=false;
		mainpanel.revalidate();
		mainpanel.repaint();
    }
    else if (source==P.ProfCoursList)
    {
    	System.out.println("P.ProfCoursList");
    	String courseid=P.professorcourses[P.ProfCoursList.getSelectedIndex()][0];
    	out.println("P;"+Accountid+";Get_Students;"+courseid);
		try{
			String line = in.readLine();
			System.out.println(line);
			mainpanel.removeAll();
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
	   	    mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
	     	mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
		} catch (IOException e){
			 System.out.println("Read failed");
			 System.exit(1);
		}
		
		P.submitbtn.addActionListener(this);
		mainpanel.revalidate();
		mainpanel.repaint();
    }
    
    
    else if (source==P.ProfCoursList)
    {
    	System.out.println("P.ProfCoursList");
    	String courseid=P.professorcourses[P.ProfCoursList.getSelectedIndex()][0];
    	out.println("P;"+Accountid+";Get_Students;"+courseid);
		try{
			String line = in.readLine();
			System.out.println(line);
			mainpanel.removeAll();
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
	   	    mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
	     	mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
		} catch (IOException e){
			 System.out.println("Read failed");
			 System.exit(1);
		}
		
		P.submitbtn.addActionListener(this);
		mainpanel.revalidate();
		mainpanel.repaint();
    }
    else if (source==P.ProfCoursList2)
    {
    	
    	String courseid= P.professorcourses[P.ProfCoursList2.getSelectedIndex()][0];
     	out.println("P;"+Accountid+";courselog;"+courseid);
    		try{
    	       	   String line = in.readLine();
    	       	  // TA.PII(line);
    			//String line="fad";
    	           mainpanel.removeAll();
    	 	   	   mainpanel.add( northjpanel , BorderLayout.NORTH );
    	       	   mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
    	       	   mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
    	       	   //System.out.println("dfaad");
    	   		 } catch (Exception e){
    	   			 System.out.println("Read failed");
    	          	 System.exit(1);
    	          }
    	     	finally{
    	     		mainpanel.revalidate();
    	         	mainpanel.repaint();
    	     	}
    	
//    	System.out.println("P.ProfCoursList");
//    	String courseid=P.professorcourses[P.ProfCoursList2.getSelectedIndex()][0];
//    	out.println("P;"+Accountid+";Get_Students;"+courseid);
//		try{
//			String line = in.readLine();
//			System.out.println(line);
//			mainpanel.removeAll();
//	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
//	   	    mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
//	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
//	     	mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
//		} catch (IOException e){
//			 System.out.println("Read failed");
//			 System.exit(1);
//		}		
		mainpanel.revalidate();
		mainpanel.repaint();
    }
    else if (source ==P.submitbtn)
    {
	    	int s=P.DataBaseTable.getSelectedRow();
	    	if (s!=-1)
	    	{
	    	System.out.println("fahadfromsubmite");
	    	String courseid=P.professorcourses[P.ProfCoursList.getSelectedIndex()][0];
	    	String studentusername=P.CurrentStudentList[s][0];
	    	String Grade=P.gradecombo.getSelectedItem().toString();
	    	out.println("P;"+Accountid+";SubmitGrade;"+courseid+";"+studentusername+";"+Grade);
			try{
				String line = in.readLine( );
				System.out.println(line);
				updateStudentList(P.ProfCoursList.getSelectedIndex());	
			} catch (IOException e ){
				 System.out.println( "Read failed" );
				 System.exit( 1 );
			}
	    	}
    }
   
     if ( source==RoleList )
	    {
	   	  s =RoleList.getSelectedItem( ).toString( );
	   	 int i=RoleList.getSelectedIndex( );
	   	 if( s.equals( "Student" ) )	
			{		
				mainpanel.removeAll( );
	   		 	mainpanel.add( northjpanel , BorderLayout.NORTH );
	   	      	mainpanel.add( S.SMenueInterface( ) , BorderLayout.WEST );
		   	 }
			else if (s.equals( "Professor" ))
			{
					mainpanel.removeAll( );
			   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
			   	    //mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
	   	     		mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
			}
			else if (s.equals( "Ta" ))
			{
					mainpanel.removeAll( );
			   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
			   	    //mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
	   	     		mainpanel.add( TA.SMenueInterface( ) , BorderLayout.WEST );
	   	     	
			}
	     mainpanel.revalidate();
	   	 mainpanel.repaint();
	   	 RoleList.setSelectedIndex( i );
	     
	    }
    
     // ###################################### START: TA USER INTERFACE ######################################
     if( source==TA.PII )
     {
     	out.println("T;" + Accountid + ";GET_PII");
     	try{
     		
       	   String line = in.readLine();
       	   TA.PII(line);
           mainpanel.removeAll();
 	   	   mainpanel.add( northjpanel , BorderLayout.NORTH );
       	   mainpanel.add( TA.getaction( line ), BorderLayout.CENTER );
       	   mainpanel.add( TA.SMenueInterface( ) , BorderLayout.WEST );
   		 } catch (IOException e){
   			 System.out.println("Read failed");
          	 System.exit(1);
          }
     	finally{
     		mainpanel.revalidate();
         	mainpanel.repaint();
     	}
     }
     if ( source ==P.CourseLog)
     {
    	 P.check=true;
    	String courseid= P.professorcourses[P.ProfCoursList2.getSelectedIndex()][0];
     	out.println("P;"+Accountid+";courselog;"+courseid);
    		try{
    	       	   String line = in.readLine();
    	       	  // TA.PII(line);
    			//String line="fad";
    	           mainpanel.removeAll();
    	 	   	   mainpanel.add( northjpanel , BorderLayout.NORTH );
    	       	   mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
    	       	   mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
    	       	   //System.out.println("dfaad");
    	   		 } catch (Exception e){
    	   			 System.out.println("Read failed");
    	          	 System.exit(1);
    	          }
    	     	finally{
    	     		mainpanel.revalidate();
    	         	mainpanel.repaint();
    	     	}
    	 
     }
     if( source == TA.ShowCorses )
     {
     	String courseid = null;
     	for (int i=0;i<Courses.length;i++)
     	{	
        		if (Courses[i][1].equals("Ta")){
     				courseid=Courses[i][0];
     					break;
     			}
     	    	
     	}
     	out.println("T;"+Accountid+";Get_Students;"+courseid);
 		try{
 			String line = in.readLine();
 			System.out.println("TA: "+line);
 			mainpanel.removeAll();
 			
 	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
 	   	   
 	   	    mainpanel.add( TA.getaction( line ), BorderLayout.CENTER );
 	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
 	     	mainpanel.add( TA.SMenueInterface( ) , BorderLayout.WEST );
 		} catch (IOException e){
 			 System.out.println("Read failed");
      	 System.exit(1);
 		}
// 		if (firsttime){
 		TA.submitbtn.addActionListener(this);
 		//P.DataBaseTable.addMouseListener(this);
// 		}
 		firsttime=false;
 		mainpanel.revalidate();
 		mainpanel.repaint();
     }
     else if (source==TA.ProfCoursList)
     {
    	 System.out.println("TA.ProfCoursList");
     	String courseid=TA.professorcourses[TA.ProfCoursList.getSelectedIndex()][0];
     	System.out.println("courseid : "+courseid);
     	out.println("T;"+Accountid+";Get_Students;"+courseid);
 		try{
 			String line = in.readLine();
 			System.out.println(line);
 			mainpanel.removeAll();
 	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
 	   	    mainpanel.add( TA.getaction( line ), BorderLayout.CENTER );
 	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
 	     	mainpanel.add( TA.SMenueInterface( ) , BorderLayout.WEST );
 		} catch (IOException e){
 			 System.out.println("Read failed");
 			 System.exit(1);
 		}
 		
 		TA.submitbtn.addActionListener(this);
 		mainpanel.revalidate();
 		mainpanel.repaint();
     }
     else if (source ==TA.submitbtn)
     {
 	    	int s=TA.DataBaseTable.getSelectedRow();
 	    	if (s!=-1)
 	    	{
 	    	System.out.println("fahadfromsubmite");
 	    	String courseid=TA.professorcourses[TA.ProfCoursList.getSelectedIndex()][0];
 	    	String studentusername=TA.CurrentStudentList[s][0];
 	    	String Grade=TA.gradecombo.getSelectedItem().toString();
 	    	out.println("T;"+Accountid+";SubmitGrade;"+courseid+";"+studentusername+";"+Grade);
 			try{
 				String line = in.readLine( );
 				System.out.println(line);
 				updateStudentListt(TA.ProfCoursList.getSelectedIndex());	
 			} catch (IOException e ){
 				 System.out.println( "Read failed" );
 				 System.exit( 1 );
 			}
 	    	}
     }
   
     
     // ###################################### END: TA USER INTERFACE ######################################

//    else 
//    {
//    	// to fill the table 
//    	out.println("P;get_List;"+Accountid+";"+P.ProfCoursList.getSelectedItem());
//    	try{
//				String line = in.readLine();
//				System.out.println(line);
//				//mainpanel.removeAll();
//	     		//mainpanel.add( P.getaction( line ), BorderLayout.NORTH );
//				//foramttheline();
//	     		//mainpanel.add( P.Studenttabel(),BorderLayout.CENTER);
//	     		//mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
//    		System.out.println("asd");
//		} catch (Exception e){
//			 System.out.println("Read failed");
//     	 System.exit(1);
//		}
//		mainpanel.revalidate();
//		mainpanel.repaint();
//    }
    if(source== S.Logout)
    {
    	System.exit(-1);
    }  
    if(source== P.Logout)
    {
    	System.exit(-1);
    }  
 	}
 
 
  //   if(source == Tran){
    	 //Send data over socket
    	 //s.Id=3;
    	 //s.Name="fahad";
    	 //name.setText(s.Name+" acting as"+Roal);
    	 //panel1.removeAll();
//         panel1.revalidate();
//         panel1.repaint();
//    	 s.course="CSci5412";
        // String text = textField.getText();
        //  int text = Integer.parseInt(textField.getText());
    	// int i=3;
//        out.println(s.get_Tran(s.course));
        // out.println(s.get_courses(s.Id));
    	// out.print(s);
    
    	// ObjectOutputStream oos = new ObjectOutputStream(s);  
    	// oos.writeObject(os);
	    //textField.setText(new String(""));
        //Receive text from server
//       try{
//    	  String line = in.readLine();
//          System.out.println("Text received :" + line);
//          catch_response(line);
//         
//		 // textField.setText(line);
//       } catch (IOException e){
//	 System.out.println("Read failed");
//       	 System.exit(1);
      // }
   //  }
     
//     else if(source == PII)
//     {
//    	 s.Id=3;
//    	 try{
//    		 //String line=in.readLine();
//    		 piidesplay("s;asd;asd;asd;as");
//    	 }catch (Exception e)
//    	 {
//    		 e.printStackTrace();
//    		 System.exit(1);
//    	 }
//    	
//     }
//     else
//    	 ;
  //}
  
  public boolean listenSocket(String s){
//Create socket connection
	     try{
	    	 SSLSocketFactory sslFact =
		               (SSLSocketFactory)SSLSocketFactory.getDefault();
	    	 SSLSocket sock = (SSLSocket) sslFact.createSocket(s, 4448);
	    	 System.out.println("handshaking...");
	    	 sock.startHandshake();
	    	 
	       //socket = new Socket(s, 4447);
	       out = new PrintWriter(sock.getOutputStream(), true);
	       in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
	       Statuslbl.setText("");
	     } catch (UnknownHostException e) {
	    	 pan.setVisible(true);
		    Statuslbl.setText("[ Not valid IP Server ]");
	       System.out.println("Unknown host...");
	       //System.exit(1);
	       return false;
	     } catch  (IOException e) {
	       System.out.println("No I/O");
	       //System.exit(1);
	       return false;
	     }
	     return true;
  }

   public static void main(String[] args){
         frame = new main();
         frame.server();
         //frame.firstpanel();
         //frame.listenSocket(txt.getText());         
  }
   
   public void ViewTranscript(String a,int start){
	   
//	   String[] line =a.split(";");
//	   JPanel Grade=new JPanel();
//	   Grade.setLayout(new GridLayout((line.length/2),2));
//	   JLabel lbl = new JLabel("Traaaaaaaaaaaan");
		/* String[] line=a.split(";");		 
		 JPanel Grade=new JPanel();
		 Grade.setLayout(new GridLayout((line.length/2),2));
		 JLabel[] forlooplabel = new JLabel[line.length];
		 for (int i=start;i<line.length;i++){
		 forlooplabel[i]=new JLabel(line[i]+"      ");
	     Grade.add(forlooplabel[i]);
		 }
		 */
		 //return(Grade);
//		 panel2.removeAll();
//         panel2.add(s.ViewTranscript(a,2));
//         panel2.revalidate();
//         panel2.repaint();
   }
   private void catch_response(String a)
   {
	   String[] line=a.split(";");
	   if (line[0].equals("S"))
	   {
		   if(line[2].equals("get_transcript"))
		   { ViewTranscript( a , 2 );
		   }
		   else if (line[2].equals("get_pii"))
		   {
			   	// panel2.removeAll();
		         //panel2.add(s.get_Pii(a));
		         //panel2.revalidate();
		         //panel2.repaint();
		   }
		   else
			   ;
	   }
	   
	   if (line[0].equals("my_info"))
	   {
		   if (Role=="Professor")
		   {
			   P.PII(a);
		   }
		   else if (Role=="Student")
		   {
			   S.PII(a);
		   }
		   
	   }
	 
   }
   private void piidesplay(String a)
   {
//	   panel2.removeAll();
//	   panel2.add(s.get_Pii(a));
//       panel2.revalidate();
//       panel2.repaint();
   }
   private int Countrole(String line)
   {
	   String[] courses=line.split(";");
		 int num=0;
		if (!courses[1].equals("null"))
		{		 
		 String[] s=new String[(courses.length-1)/3];
		 if (!courses.equals(null))
		 {	
			 Courses=new String[(courses.length-1)/3][3];//row,columns
			 boolean exist;
			 int index=0;
			 for (int i=2;i<courses.length;i+=3)
				 {
				 Courses [index][0] = courses [i-1];
				 Courses [index][1] = courses [i];
				 Courses [index][2] = courses [i+1];
				 index++;
				 exist=false;
				 	if( num > 0 )
						{	
				 		boolean check=true;
							 for(int l=0;l<num;l++)
								{
								 if(courses[i].equals( s[ l ]) )
								 {check=false;
									 break;
								 }
								}
							 if (check)
									{
									 	s[num]= courses[i];
									 	exist=true;	 
									}
						}
					 else
					 {
						 s[num]= courses[i];
						 exist=true;
					 }
					
					if(exist)
					{
						num++;
					}
				 }
		 }
		 	Roles= new String[num];
			for(int i=0;i<num;i++)
			{
				if(s[i].equals(null))
					break;
				Roles[i]=s[i];
			}
		}
		
		return(num);
   }
   public JPanel Northmenue( )
	{	JPanel SMenueInterface = new JPanel( );
	 	SMenueInterface.setLayout( new GridLayout( 1, 5 ) );
	 	SMenueInterface.add(new JLabel("Welcome "+Accountid));
	 	SMenueInterface.add(new JLabel(""));
	 	SMenueInterface.add(new JLabel(""));
	 	SMenueInterface.add(new JLabel(""));
	 	if (numberofroles>1)
	 	{
	 	SMenueInterface.add(RoleList);
	 	}
	 	RoleList.addActionListener( this );
		return( SMenueInterface );
	}
   public JPanel Northmenuee( )
	{	JPanel SMenueInterfacee = new JPanel( );
	 	//SMenueInterfacee.setLayout( new GridLayout( 1, 5 ) );
	 	SMenueInterfacee.add(new JLabel("Welcome "+Accountid + ". You are not assigned to any course."));
		return( SMenueInterfacee );
	}
   public void SetRoles( )
   {
		if (numberofroles>0)
	 	{
		 	if (Roles.length>0)
		 	{
		 	 for(int i=0;i<Roles.length;i++)
				 RoleList.addItem(Roles[i]);		 	 
		 	}		 
	 	}
   }
   public JPanel RigesterAccountinterface()
	{
		// the interface for creating new account 
		 JLabel Usernamelb 			= new JLabel("User Name ");
		 JLabel Passlb 				= new JLabel("Password ");
		 JLabel Useremaillb 		= new JLabel("User Email");
		 JLabel Userfullnamelb 		= new JLabel("Name ");
		 JLabel Useraddresslb 		= new JLabel("Address ");
		 JLabel Userphonelb 		= new JLabel("Phone ");
		 
		 JPanel createAccount		=new JPanel();
		 createAccount.setBorder(BorderFactory.createTitledBorder("Sign-up Form"));
		 Dimension FrameSize=new Dimension( 200 , 300 );
		 
		 createAccount.setPreferredSize(FrameSize);
		 createAccount.setLayout(new GridLayout(8,4));
		 createAccount.add(Usernamelb);
		 createAccount.add(Username);
		 createAccount.add(Passlb);
		 createAccount.add(UserPass);
		 createAccount.add(Useremaillb);
		 createAccount.add(Useremail);
		 createAccount.add(Userfullnamelb);
		 createAccount.add(Userfullname);
		 createAccount.add(Useraddresslb);
		 createAccount.add(Useraddress);
		 createAccount.add(Userphonelb);
		 createAccount.add(Userphone);
		 createAccount.add(Rigester);
		 createAccount.add(StatusReport);
		 createAccount.validate();
		 
		 return(createAccount);
	}
   public void updateStudentList(int courseindex)
   {
   	String courseid=P.professorcourses[P.ProfCoursList.getSelectedIndex()][0];
   	P.ProfCoursList.setSelectedIndex(courseindex);
   	out.println("P;"+Accountid+";Get_Students;"+courseid);
		try{
			String line = in.readLine( );
			System.out.println( line );
			mainpanel.removeAll( );
			P.Courses=Courses;
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
	   	    mainpanel.add( P.getaction( line ), BorderLayout.CENTER );
	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
	     	mainpanel.add( P.SMenueInterface( ) , BorderLayout.WEST );
		} catch (IOException e){
			 System.out.println("Read failed");
    	 System.exit(1);
		}
		P.submitbtn.addActionListener(this);

		mainpanel.revalidate();
		mainpanel.repaint();
   }
   public void updateStudentListt(int courseindex)
   {
   	String courseid=TA.professorcourses[TA.ProfCoursList.getSelectedIndex()][0];
   	TA.ProfCoursList.setSelectedIndex(courseindex);
   	out.println("T;"+Accountid+";Get_Students;"+courseid);
		try{
			String line = in.readLine( );
			System.out.println( line );
			mainpanel.removeAll( );
			TA.Courses=Courses;
	   	    mainpanel.add( northjpanel , BorderLayout.NORTH );
	   	    mainpanel.add( TA.getaction( line ), BorderLayout.CENTER );
	     //	mainpanel.add(P.Studenttabel(),BorderLayout.CENTER);
	     	mainpanel.add( TA.SMenueInterface( ) , BorderLayout.WEST );
		} catch (IOException e){
			 System.out.println("Read failed");
    	 System.exit(1);
		}
		TA.submitbtn.addActionListener(this);

		mainpanel.revalidate();
		mainpanel.repaint();
   }

public void mouseClicked(MouseEvent e) {
	// TODO Auto-generated method stub
	Object source =e.getSource();

	if(e.getClickCount() == 2)
	{
		if (source==P.DataBaseTable)
		{
		JFrame frame2 = new JFrame("FrameDemo");
		frame2.getContentPane().add(P.seeStudenttabel(), BorderLayout.CENTER);
		frame2.pack();
		frame2.setLocationRelativeTo(P.J);
		frame2.setVisible(true);
		System.out.println("fa");
		}
		else if (source==TA.DataBaseTable)
		{
			JFrame frame2 = new JFrame("FrameDemo");
			frame2.getContentPane().add(TA.see(), BorderLayout.CENTER);
			frame2.pack();
			frame2.setLocationRelativeTo(TA.J);
			frame2.setVisible(true);
			System.out.println("fa");
			
		}
		else
		{
			JFrame frame2 = new JFrame("FrameDemo");
			frame2.getContentPane().add(P.seelogtabel(), BorderLayout.CENTER);
			frame2.pack();
			frame2.setLocationRelativeTo(P.J);
			frame2.setVisible(true);
			System.out.println("fa");
		}
		//frame2.setDefaultCloseOperation(frame2.EXIT_ON_CLOSE);
	}
//	if (source==P.DataBaseTable)
//	{
//		if (P.check)
//		P.DataBaseTableMouseReleasedlogtabel();
//		else
//		P.DataBaseTableMouseReleasedStudenttabel();
//	}
	
	
	
}

public void mouseEntered(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

public void mouseExited(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

public void mouseReleased(MouseEvent e) {
	s =RoleList.getSelectedItem( ).toString( );
	Object source =e.getSource();
	//if(s.equals("Professor"))
	//P.DataBaseTableMouseReleasedStudenttabel();
	 if(s.equals("Ta"))
		TA.DataBaseTableMouseReleased();	
	
//	else if(source==P.proflogtable)
//	P.DataBaseTableMouseReleasedlogtabel();
	else if(source==P.DataBaseTable)
		P.DataBaseTableMouseReleasedStudenttabel();
	//else if (source==P.proflogtable)
		//P.DataBaseTableMouseReleased2();
}


public void mousePressed(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

}